package cn.stylefeng.guns.modular.goodsEvaluate.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.goodsEvaluate.entity.TaoGoodsEvaluate;
import cn.stylefeng.guns.modular.goodsEvaluate.model.params.TaoGoodsEvaluateParam;
import cn.stylefeng.guns.modular.goodsEvaluate.service.TaoGoodsEvaluateService;
import cn.stylefeng.guns.modular.wxuser.entity.TaoWxUser;
import cn.stylefeng.guns.modular.wxuser.service.TaoWxUserService;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 商品评价控制器
 *
 * @author lcw
 * @Date 2019-11-01 14:01:23
 */
@Controller
@RequestMapping("/taoGoodsEvaluate")
public class TaoGoodsEvaluateController extends BaseController {

    private String PREFIX = "/goodsEvaluate";


    /**
     * 商品评价表
     */
    @Autowired
    private TaoGoodsEvaluateService taoGoodsEvaluateService;

    /**
     * 商品表
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * 微信用户表
     */
    @Autowired
    private TaoWxUserService taoWxUserService;

    /**
     * 跳转到主页面
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/taoGoodsEvaluate.html";
    }

    /**
     * 新增页面
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/taoGoodsEvaluate_add.html";
    }

    /**
     * 编辑页面
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/taoGoodsEvaluate_edit.html";
    }

    /**
     * 新增接口
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TaoGoodsEvaluateParam taoGoodsEvaluateParam) {
        this.taoGoodsEvaluateService.add(taoGoodsEvaluateParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TaoGoodsEvaluateParam taoGoodsEvaluateParam) {
        this.taoGoodsEvaluateService.update(taoGoodsEvaluateParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TaoGoodsEvaluateParam taoGoodsEvaluateParam) {
        this.taoGoodsEvaluateService.delete(taoGoodsEvaluateParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TaoGoodsEvaluateParam taoGoodsEvaluateParam) {
        TaoGoodsEvaluate detail = this.taoGoodsEvaluateService.getById(taoGoodsEvaluateParam.getWxOpenId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author lcw
     * @Date 2019-11-01
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TaoGoodsEvaluateParam taoGoodsEvaluateParam) {
        LayuiPageInfo pageBySpec = this.taoGoodsEvaluateService.findPageBySpec(taoGoodsEvaluateParam);
        QueryWrapper<TaoGoodsEvaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        List<TaoGoodsEvaluate> list = taoGoodsEvaluateService.list(queryWrapper);
        for(TaoGoodsEvaluate row:list){
            try
            {
                row.setGoodNames(goodsService.getById(row.getGoodId()).getFullname());
                row.setWeixinName(taoWxUserService.getOne(Wrappers.<TaoWxUser>lambdaQuery().eq(TaoWxUser::getOpenId, row.getWxOpenId())).getNickName());
            }catch(NullPointerException e)
            {
                System.out.println("发生异常的原因为 :"+e.getMessage());
            }
        }
        pageBySpec.setData(list);
        return pageBySpec;
    }





}


